package com.harmony.pilot.completion.client;

import com.harmony.pilot.common.dto.CodeContext;
import com.harmony.pilot.common.dto.CompletionItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Collections;
import java.util.List;

/**
 * 数据服务客户端
 */
@Slf4j
@Component
public class DataServiceClient {
    
    private final WebClient webClient;
    
    public DataServiceClient(@Value("${service.data.url}") String dataServiceUrl) {
        this.webClient = WebClient.builder()
                .baseUrl(dataServiceUrl)
                .build();
    }
    
    /**
     * 从CodeQL查询符号
     */
    public List<CompletionItem> querySymbols(CodeContext context) {
        try {
            return webClient.post()
                    .uri("/data/codeql/symbols")
                    .bodyValue(context)
                    .retrieve()
                    .bodyToFlux(CompletionItem.class)
                    .collectList()
                    .block();
        } catch (Exception e) {
            log.error("Failed to query symbols from data service", e);
            return Collections.emptyList();
        }
    }
    
    /**
     * 从Neo4j查询相关符号
     */
    public List<CompletionItem> queryRelatedSymbols(CodeContext context) {
        try {
            return webClient.post()
                    .uri("/data/neo4j/related")
                    .bodyValue(context)
                    .retrieve()
                    .bodyToFlux(CompletionItem.class)
                    .collectList()
                    .block();
        } catch (Exception e) {
            log.error("Failed to query related symbols from Neo4j", e);
            return Collections.emptyList();
        }
    }
}

